package com.shangxia.dreaming.business;

import com.shangxia.dreaming.common.util.txtUtil.ReadTXT;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import static com.shangxia.dreaming.common.content.ConditionContent.*;

/**
 * @ProjectName: dreaming
 * @ClassName: PrePare
 * @Time: 2020/10/5
 * @Author: zhouy
 * @Description:
 * @Experience:
 */

@Slf4j
@Component
public class PrePareJob {

    /**
     * @param stages        总（真号）期数
     * @param sonCodeLength 一期号码真号个数
     * @param sumCodeLength 一期号码总个数
     * @return
     */
    public List<Integer>[][] turnTable(List<List<Integer>> realCodeLists, Integer stages, Integer sonCodeLength, Integer sumCodeLength) {
        List orderList = new ArrayList(sumCodeLength);
        for (int i = 0; i < sumCodeLength; i++) {
            orderList.add(i + 1);
        }
        List<Integer>[][] arr = new ArrayList[stages][stages];
        log.info("计算层数=" + Math.min(stages, MUST_TURN_TABLE_COLUMN));
        for (int h = 0; h < stages; h++) {
            if (h == 0) {
                List<Integer> list = new ArrayList<>(orderList);
                list.removeAll(realCodeLists.get(0));
                list.addAll(realCodeLists.get(0));
                arr[0][0] = list;
            }
            int column = Math.min(stages, MUST_TURN_TABLE_COLUMN);
            //修改最大层数
            for (int l = 0; l < column; l++) {
                if (l > h || (h == 0 && l == 0)) {
                    break;
                }
                if (l == 0) {
                    List<Integer> list1 = new ArrayList<>();
                    List<Integer> list2 = new ArrayList<>(arr[h - 1][0]);
                    list2.removeAll(realCodeLists.get(h));
                    list1.addAll(list2);
                    list1.addAll(realCodeLists.get(h));
                    arr[h][l] = list1;
                } else {
                    List<Integer> list1 = new ArrayList<>(arr[h][l - 1]);
                    //虚真号
                    List<Integer> list2 = new ArrayList<>();
                    for (int i = 0; i < sonCodeLength; i++) {
                        //前一层真号在前一层上上一期所占的位置是本层虚真号
                        list2.add(arr[h - 1][l - 1].indexOf(list1.get(sumCodeLength - sonCodeLength + i)) + 1);
                    }
                    List list;
                    if (arr[h - 1][l] == null) {
                        list = new ArrayList<>(orderList);
                    } else {
                        list = new ArrayList<>(arr[h - 1][l]);
                    }
                    list.removeAll(list2);
                    list.addAll(list2);
                    arr[h][l] = list;
                }
            }
        }
        return arr;
    }



    public List<Integer>[][] turnTable0(List<List<Integer>> realCodeLists, Integer stages, Integer sonCodeLength, Integer sumCodeLength) {
        List orderList = new ArrayList(sumCodeLength);
        for (int i = 0; i < sumCodeLength; i++) {
            orderList.add(i);
        }
        List<Integer>[][] arr = new ArrayList[stages][stages];
        for (int h = 0; h < stages; h++) {
            if (h == 0) {
                List<Integer> list = new ArrayList<>(orderList);
                list.removeAll(realCodeLists.get(0));
                list.addAll(realCodeLists.get(0));
                arr[0][0] = list;
            }
            int column = Math.min(stages, MUST_TURN_TABLE_COLUMN);
            //修改最大层数
            for (int l = 0; l < column; l++) {
                if (l > h || (h == 0 && l == 0)) {
                    break;
                }
                if (l == 0) {
                    List<Integer> list1 = new ArrayList<>();
                    List<Integer> list2 = new ArrayList<>(arr[h - 1][0]);
                    list2.removeAll(realCodeLists.get(h));
                    list1.addAll(list2);
                    list1.addAll(realCodeLists.get(h));
                    arr[h][l] = list1;
                } else {
                    List<Integer> list1 = new ArrayList<>(arr[h][l - 1]);
                    //虚真号
                    List<Integer> list2 = new ArrayList<>();
                    for (int i = 0; i < sonCodeLength; i++) {
                        //前一层真号在前一层上上一期所占的位置是本层虚真号
                        list2.add(arr[h - 1][l - 1].indexOf(list1.get(sumCodeLength - sonCodeLength + i)));
                    }
                    List list;
                    if (arr[h - 1][l] == null) {
                        list = new ArrayList<>(orderList);
                    } else {
                        list = new ArrayList<>(arr[h - 1][l]);
                    }
                    list.removeAll(list2);
                    list.addAll(list2);
                    arr[h][l] = list;
                }
            }
        }
        return arr;
    }

}
